Skip to content

Throttled progress/cancel callback for load_obj_buf - #79

Open
virtualritz wants to merge 1 commit into
Twinklebear:masterfrom
virtualritz:pr/progress-callback
Open

Throttled progress/cancel callback for load_obj_buf#79
virtualritz wants to merge 1 commit into
Twinklebear:masterfrom
virtualritz:pr/progress-callback

Conversation

@virtualritz

Copy link
Copy Markdown
Contributor

Summary

  • LoadOptions gains progress_callback: Option<LoadProgressCallback> (Arc<dyn Fn(&LoadProgress) -> ControlFlow<()> + Send + Sync>), an options field rather than a second _with_progress function, so a caller can observe throttled progress and cooperatively cancel a load of a large OBJ buffer.
  • Invoked every 1000 lines inside load_obj_buf's parse loop. Returning ControlFlow::Break stops the load and returns the new LoadError::Cancelled.
  • No behavior change when progress_callback is None (the default): verified by a test comparing output with and without a no-op callback.
  • load_obj_buf_async (the futures/tokio variants) is intentionally untouched — that's a materially different code path deserving its own design pass.
  • LoadOptions loses its Copy derive (Arc isn't Copy); every existing call site in this crate already passes &LoadOptions, so nothing else needed to change.

Motivated by a real use case: an interactive mesh-editing tool importing large OBJ files needed a way to show progress and let a user cancel a slow import without the caller being able to observe anything until the whole call returned.

Test plan

  • cargo test --all-features: 21 lib tests + 5 doctests pass, including 3 new tests (test_progress_callback_noop_matches_no_callback, test_progress_callback_cancels_load, test_progress_callback_is_throttled)
  • cargo fmt --all -- --check: clean
  • cargo clippy --all-targets --all-features -- -D warnings: clean (exit 0)

LoadOptions gains progress_callback: Option<LoadProgressCallback>
(Arc<dyn Fn(&LoadProgress) -> ControlFlow<()> + Send + Sync>, an options
field rather than a second _with_progress function), invoked every 1000
lines during load_obj_buf's parse loop. Returning ControlFlow::Break stops
the load and returns the new LoadError::Cancelled. No behavior change when
progress_callback is None (the default): verified by a test comparing
output with and without a no-op callback. load_obj_buf_async is untouched.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant